<template>
  <a-modal
    :title="title"
    :width="460"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    okText="保存"
    wrapClassName="ant-modal-cust-warp">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="定制项"
                hasFeedback >
          <a-select @change="show_canshulist(select_topcategory1)" v-model="select_topcategory1" style="color: #827d7d" placeholder="请选择定制项">
            <a-select-option :value="item.id" v-for="item in categoryList" >{{ item.parameter }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="撞色参数"
                hasFeedback >
          <a-select v-model="select_huchi1" style="color: #827d7d" placeholder="请选择撞色参数">
            <a-select-option :value="item.id" v-for="item in dingzhi_canshuList" >{{ item.categoryName }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="撞色项名称">
          <a-input placeholder="请输入撞色项名称" v-model="huchi_name"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="撞色编号"
          hasFeedback >
          <a-input placeholder="请输入撞色编号" v-model="huchi_code" v-show="this.title=='编辑撞色项'" disabled="disabled"/>
          <a-input placeholder="请输入撞色编号" v-model="huchi_code" v-show="this.title=='新增撞色项'"/>
        </a-form-item>


        <a-form-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="撞色图片"
                hasFeedback >
          <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-image-upload class="avatar-uploader" text="上传" v-model="fileList" style="margin-top: 8px;"></j-image-upload>
          </a-form-item>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注"
          style="margin-top: -50px;" >
          <a-textarea placeholder="请输入备注信息" v-model="remark" rows="3"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction, postAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JImageUpload from '../../../components/jeecg/JImageUpload'

  export default {
    name: "JeecgDemoModal",
    components: {
      JImageUpload
    },
    data () {
      return {
        huchi_name:'',
        huchi_code:'',
        select_topcategory1:'请选择定制项',
        select_huchi1:'请选择参数',
        fileList: [],
        remark:'',
        canshu2:'',
        dataDetail:[],//数据对象
        title:"操作",
        visible: false,
        model: {},
        validatorRules:{
          roleName:{
            rules: [
              { required: true, message: '请输入角色名称!' },
              { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
            ]},
          roleCode:{
            rules: [
              { required: true, message: '请输入角色名称!'},
              { min: 0, max: 64, message: '长度不超过 64 个字符', trigger: 'blur' },
              { validator: this.validateRoleCode}
            ]},
          description:{
            rules: [
              { min: 0, max: 126, message: '长度不超过 126 个字符', trigger: 'blur' }
            ]}
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        statuslist: [
          {
            id: '1',
            name: '允许'
          },
          {
            id: '2',
            name: '禁止'
          }
        ],
        categoryList: [],//定制列表
        dingzhi_canshuList: [],//定制下的参数列表
        confirmLoading: false,
        form: this.$form.createForm(this),
        url: {
          query_detail:"/admin/parameter/test_zhuangseDetail",
          add: "/admin/parameter/add_zhuangse",
          edit: "/admin/parameter/update_zhuangse",
          query_dingzhiList:"/admin/parameter/query_dingzhiBypingleiId",
          canshu_list: "/admin/parameter/test_dignzhipartmer",
          allcanshu_dingzhi:"/admin/parameter/test_all_dignzhipartmer"
        },
      }
    },
    computed: {

    },
    created () {

    },
    methods: {
      edit (record) {
        this.showdingzhi_list();//查询定制项
        this.huchi_name='';
        this.huchi_code='';
        this.select_topcategory1='请选择定制项';
        this.select_huchi1='请选择撞色参数';
        this.remark='';
        this.fileList = '';
        this.model = Object.assign({}, record);
        this.visible = true;//开启弹框
        if(record=="0"){
          this.title="新增撞色项"
        }else {
          this.title="编辑撞色项"
          this.show_datainfodetail(this.model.id);
        }
      },
      showdingzhi_list() {//查询定制项列表
        let params = {category_id:this.$route.query.category_id};
        getAction(this.url.query_dingzhiList,params).then((res)=>{
          if(res.success){
            this.categoryList = res.result;
          }else {
            this.categoryList = [];
          }
        });
      },
      show_canshulist(dingzhiid) {
        let params = {module_id:dingzhiid};
        getAction(this.url.allcanshu_dingzhi,params).then((res)=>{
          console.info(res);
          if(res.success){
            this.select_huchi1="请选择参数"
            this.dingzhi_canshuList = res.result;
          }else {
            this.$message.error("查询失败2");
          }
        });
      },
      show_datainfodetail(id) {//查询详情
        let params = {id:id};
        getAction(this.url.query_detail,params).then((res)=>{
          if(res.success){
            this.huchi_name=res.result.contrastColorName;
            this.huchi_code=res.result.code;
            this.select_topcategory1=res.result.dingzhiName;
            this.select_huchi1=res.result.categoryName;
            this.remark=res.result.remark;
            setTimeout(() => {
              this.fileList = res.result.contrastColorImg;
            }, 5)
          }else {
            this.$message.error("查询失败2");
          }
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if(this.huchi_name==null || this.huchi_name==''){
            this.$message.warning("请输入撞色项名称");
            return;
          }else if(this.huchi_code==null || this.huchi_code==''){
            this.$message.warning("请输入撞色编号");
            return;
          }
          // else if(this.select_topcategory1=='' || this.select_topcategory1=='请选择定制项'){
          //   this.$message.warning("请选择定制项");
          //   return;
          // }else if(this.select_huchi1=='' || this.select_huchi1=='请选择撞色参数'){
          //   this.$message.warning("请选择撞色参数");
          //   return;
          // }
          else if(this.fileList == null || this.fileList.length <= 0){
            this.$message.warning("请上传品类图片");
            return;
          }
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            let params = '';
            if(this.model.id!=null && this.model.id!=''){
              httpurl+=this.url.edit;
              method = 'put';
              params = {contrastColorName:this.huchi_name,remark:this.remark,code:this.huchi_code,
                dingzhiId:this.select_topcategory1,contrastColorImg:this.fileList,categoryId:this.select_huchi1,id:this.model.id}
            }else{
              httpurl+=this.url.add;
              method = 'post';
              params = {contrastColorName:this.huchi_name,remark:this.remark,code:this.huchi_code,
                dingzhiId:this.select_topcategory1,contrastColorImg:this.fileList,categoryId:this.select_huchi1,
                topId:this.$route.query.category_id}
            }
            httpAction(httpurl,params,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.confirmLoading = false;
                that.close();
              }else{
                that.$message.warning(res.message);
                that.confirmLoading = false;
              }
            }).finally(() => {
              // that.confirmLoading = false;
              // that.$emit('ok');
              // that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>